package com.common;

import org.apache.log4j.Logger;
//import org.springframework.util.ResourceUtils;
import java.io.*;
import java.util.*;

/**
 * properties 工具类
 */
public class PropertiesUtil {

	/**
	 * 输出properties的key和value
	 */
//	public static void printProp(Properties properties) {
//		for (String key : properties.stringPropertyNames()) {
//			System.out.println(key + "=" + properties.getProperty(key));
//		}
//		Set<Object> keys = properties.keySet();
//		for (Object key : keys) {
//			System.out.println(key.toString() + "=" + properties.get(key));
//		}
//		Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
//		for (Map.Entry<Object, Object> entry : entrySet) {
//			System.out.println(entry.getKey() + "=" + entry.getValue());
//		}
//		Enumeration<?> e = properties.propertyNames();
//		while (e.hasMoreElements()) {
//			String key = (String) e.nextElement();
//			String value = properties.getProperty(key);
//			System.out.println(key + "=" + value);
//		}
//	}

	/**
	 * 将properties转换为Map集合
	 *
	 * @param properties
	 * @return
	 */
	public static Map<String, String> changeMap(MyProperties properties) {
		Map<String, String> result = new HashMap<String, String>();
		Enumeration<?> e = properties.propertyNames();
		while (e.hasMoreElements()) {
			String key = (String) e.nextElement();
			String value = properties.getProperty(key);
			result.put(key, value);
		}
		return result;
	}

	/**
	 * 读取Properties
	 *
	 * @param file
	 * @return
	 */
	public static MyProperties readOrderedPropertiesFile(File file) {
		MyProperties properties = new MyProperties();
		InputStreamReader inputStreamReader = null;
		try {
			InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
			inputStreamReader = new InputStreamReader(inputStream, "utf-8");
			properties.load(inputStreamReader);
		} catch (Exception e) {
			e.printStackTrace();
			Logger.getLogger(PropertiesUtil.class).error(e.getMessage());
		} finally {
			if (inputStreamReader != null) {
				try {
					inputStreamReader.close();
				} catch (IOException e) {
					e.printStackTrace();
					Logger.getLogger(PropertiesUtil.class).error(e.getMessage());
				}
			}
		}
		return properties;
	}

	/**
	 * 写入Properties文件
	 *
	 * @param properties
	 * @param file
	 * @param comment
	 */
	public static void writeOrderedPropertiesFile(MyProperties properties, File file, String comment) {
		OutputStreamWriter outputStreamWriter = null;
		try {
			FileOutputStream fileOutputStream = new FileOutputStream(file, false);//true表示追加打开,false每次都是清空再重写
			outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
			properties.store(outputStreamWriter, comment);
		} catch (Exception e) {
			e.printStackTrace();
			Logger.getLogger(PropertiesUtil.class).error(e.getMessage());
		} finally {
			if (outputStreamWriter != null) {
				try {
					outputStreamWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
					Logger.getLogger(PropertiesUtil.class).error(e.getMessage());
				}
			}
		}
	}

	public static void main(String[] args) throws FileNotFoundException {
//		File file = ResourceUtils.getFile("config/ATC_Config.properties");
//		MyProperties prop = readOrderedPropertiesFile(file);
//		printProp(prop);
//		prop.setProperty("config", "test "+Math.random());
//		writeOrderedPropertiesFile(prop, file, "sdfsdf");
	}
}
